<template>
   <importExcel :map="map" :xlsxMap="xlsxMap"  ref="importStu"
        @submit="submitExcel"
        ></importExcel>
</template>
<script>
import importExcel from './importExcel'
import XlsxMap from './xlsxMap'
import {importTeacher} from '@/api/teacherApi'
import {getGradeList} from '@/api/schoolApi'
export default {
    components: {
        importExcel
    },
    props: {
        schoolId: String
    },
    data(){
        return {
            map: {
               职工姓名: {col: 'name', required: true},
                性别: {col: 'gender'},
                身份证号码: {col: 'idNumber', reg: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, desc: '身份证号码不正确'},
                手机号码: {col: 'cellPhoneNum', reg: /^1[3456789]\d{9}$/, required: true, desc: '手机号码不正确', noRepeat: true},
                教学科目: {col: 'subJectName', reg: ''},
                授课班级: {col: 'importTeacherClassMaps', reg: '', desc: '班级不正确', isClass: true, isCheckGrade: true},
                担任班主任班级: {col: 'masterClass', reg: '', desc: '班级不正确', isClass: true, isCheckGrade: true},
                所在部门: {col: 'deptNameList', reg: ''}
            },
            xlsxMap: new XlsxMap()
        }
    },
    methods: {
        async importTeacherData({list, schoolId}){
            
            list = this.xlsxMap.formatData(list)
            for (var item of list) {
                if (item.gender == '男') {
                    item.gender = true
                } else {
                    item.gender = false
                }
                
                item.subJectName = this.xlsxMap.splitByAll(item.subJectName)
                item.deptNameList = this.xlsxMap.splitByAll(item.deptNameList)

            
                item.importTeacherClassMaps = this.xlsxMap.splitByAll(item.importTeacherClassMaps)
                item.masterClass = this.xlsxMap.splitByAll(item.masterClass)


                var yuList = []
                item.importTeacherClassMaps = item.importTeacherClassMaps.map((name) => {
                    var obj = {
                        className: name,
                        isChargeTeacher: false,
                        isTeach: true
                    }
                
                    return obj
                })
                item.masterClass = item.masterClass.map((name) => {
                    var obj = {
                        className: name,
                        isChargeTeacher: true,
                        isTeach: false
                    }
                    return obj
                })
                
                var yuList = []
                for (var master of item.masterClass) {
                    var isHave = false
                    for (var teach of item.importTeacherClassMaps) {
                        if (teach.className == master.className) {
                            teach.isChargeTeacher = true

                            isHave = true
                        }
                    }
                    if (!isHave) {
                        yuList.push(master)
                    }
                }
                
                item.importTeacherClassMaps = item.importTeacherClassMaps.concat(yuList)
                

                delete item.masterClass
            }
            var params = {
                schoolId: schoolId,
                body: {
                items: list
                }
            }
            console.log(params)
            await importTeacher(params)
        },
        async submitExcel(data){
            try {
                 while (true) {
                    var temp = data.splice(0, 500)
                    if (temp.length > 0) {
                         await this.importTeacherData({list: temp, schoolId: this.schoolId})
                         this.$refs.importStu.progress ++
                    } else {
                        break
                    }
                }

                this.$message.success('导入教职工数据完成')
                this.$refs.importStu.complete()
                this.$emit('complete')

                this.$store.dispatch('initSchool/putSchool', {
                     schoolId: this.schoolId,
                     initState: 2
                 })
                 
                 
            } catch (error) {
                var res = error.data
               
                var details = JSON.parse(res.error.details)
               
                this.$refs.importStu.handleError(details)
            }
        }
    },
    async mounted(){
        let res = await getGradeList({
            schoolId: this.schoolId,
            Sorting: 'orderNum'
        })
        var items = res.items
        var list = items.map(obj => obj.name)
        this.xlsxMap.gradeList = list
    }
}
</script>
<style scoped>

</style>